package com.adnaan.backend.controller.admin;

import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.PageParams;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.Article;
import com.adnaan.backend.entity.ArticleTags;
import com.adnaan.backend.entity.dto.OptionDto;
import com.adnaan.backend.service.ArticleService;
import com.adnaan.backend.service.ArticleTagsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * author: adnaan
 * create: 2025-02-19 13:13
 * Description:
 */
@Tag(name = "文章管理")
@RestControllerMapping("/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @Resource
    private ArticleTagsService tagsService;

    @PostMapping("/page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "文章分页查询")
    public Result<Page<Article>> pageQuery(@RequestBody PageParams<Article> pageParams) {
        // 1.查询参数
        Article params = pageParams.getParams();
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.like(params.getTitle() != null, Article::getTitle, params.getTitle());
        // 3.分页查询
        Page<Article> page = articleService.page(pageParams.getPage(), lqw);
        page.getRecords().forEach(item -> {
            item.setTags(articleService.getTags(item.getId()));
        });
        return Result.success(page);
    }

    @PostMapping("/insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增文章数据")
    public Result<Boolean> insert(@RequestBody Article article) {
        if (articleService.save(article)) {
            return insertTags(article);
        }
        return Result.error();
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新文章数据")
    public Result<Boolean> update(@RequestBody Article article) {
        // 1.删除文章与标签的对应关系
        articleService.deleteTags(article.getId());
        if (articleService.updateById(article)) {
            return insertTags(article);
        }
        return Result.error();
    }

    private Result<Boolean> insertTags(Article article) {
        List<OptionDto> tags = article.getTags();
        if (tags != null && !tags.isEmpty()) {
            List<String> tagIds = tags.stream().map(OptionDto::getValue).toList();
            articleService.insertTags(article.getId(), tagIds);
        }
        return Result.success();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除文章数据")
    public Result<Boolean> deleteById(@PathVariable String id) {
        if (articleService.removeById(id)) {

            return Result.success();
        }
        return Result.error();
    }

    /**
     * 查询所有标签
     *
     * @return OptionDto
     */
    @GetMapping("/tags")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "查询所有标签")
    public Result<List<OptionDto>> getTags() {
        List<ArticleTags> list = tagsService.list();
        List<OptionDto> optionDtos = list.stream().map(item -> {
            OptionDto optionDto = new OptionDto();
            optionDto.setLabel(item.getTagName());
            optionDto.setValue(String.valueOf(item.getId()));
            return optionDto;
        }).toList();
        return Result.success(optionDtos);
    }
}
